//
//  UITableViewAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "UITableViewAdditions.h"


@implementation UITableView (Tint)


#pragma mark - Creating

+ (UITableView *)tableViewWithStyle:(UITableViewStyle)style {
    return [[[self alloc] initWithFrame:CGRectZero style:style] autorelease];
}

+ (UITableView *)tableViewWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    return [[[self alloc] initWithFrame:frame style:style] autorelease];
}



#pragma mark - Retriving cell

- (UITableViewCell *)dequeueReusableCellWithClass:(Class)cls {
    if (cls) {
        
        NSString *identifier = NSStringFromClass(cls);
        UITableViewCell *cell = nil;
        
        cell = [self dequeueReusableCellWithIdentifier:identifier];
        
        if (cell == nil) cell = [[[cls alloc] init] autorelease];
        
        return cell;
    }
    
    return [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
}



#pragma mark - Querying

- (UITableViewCell *)firstRowInSection:(NSUInteger)section {
    if (section < [self numberOfSections]) {
        NSUInteger rows = [self numberOfRowsInSection:section];
        if (rows > 0) {
            return [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
        }
    }
    
    return nil;
}

- (UITableViewCell *)lastRowInSection:(NSUInteger)section {
    if (section < [self numberOfSections]) {
        NSUInteger rows = [self numberOfRowsInSection:section];
        if (rows > 0) {
            return [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rows-1 inSection:section]];
        }
    }
    
    return nil;
}



#pragma mark - Refreshing

- (void)refreshSectionsWithRowFadeAnimation {
    if ([self numberOfSections]>0) {
        [self reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSections])] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)refreshSectionWithRowFadeAnimation:(NSUInteger)section {
    if (section < [self numberOfSections]) {
        [self reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
    }
}

@end
